using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using TX.Data.DSTableAdapters;

namespace TX
{
    public partial class Fatura : Base2
    {
        public Fatura()
        {
            InitializeComponent();

            tableAdapterManager.FaturaTableAdapter = new TX.Data.DSTableAdapters.FaturaTableAdapter();
            tableAdapterManager.FaturaDetayTableAdapter = new TX.Data.DSTableAdapters.FaturaDetayTableAdapter();
            tableAdapterManager.FeatureKumasTableAdapter = new TX.Data.DSTableAdapters.FeatureKumasTableAdapter();
            tableAdapterManager.RollTableAdapter = new TX.Data.DSTableAdapters.RollTableAdapter();

            tableAdapterManager.FaturaTableAdapter.ClearBeforeFill = true;
            tableAdapterManager.FaturaDetayTableAdapter.ClearBeforeFill = true;
            tableAdapterManager.FeatureKumasTableAdapter.ClearBeforeFill = true;
            tableAdapterManager.RollTableAdapter.ClearBeforeFill = true;
        }

        int faturaId;
        //public override void Kaydet()
        //{
        //    Save();
        //}

        private void Fatura_Load(object sender, EventArgs e)
        {
            try
            {
                gridView1.BeginUpdate();
                bs1 = new BindingSource(ds, "Fatura");

                bindingNavigator1.BindingSource = bs1;

                tableAdapterManager.FaturaTableAdapter.Fill(ds.Fatura);

                //dataLayoutControl1.OptionsView.HighlightFocusedItem = true;
                gridControl1.DataSource = this.bs1;
                InitGridView(gridView1, true);
                gridView1.BestFitColumns();

                //this.gridView1.OptionsBehavior.Editable = false;
                //this.gridView1.OptionsView.ColumnAutoWidth = false;
                //this.gridView1.OptionsView.EnableAppearanceOddRow = true;
                //this.gridView1.OptionsView.ShowAutoFilterRow = true;
                //gridView1.UnselectRow(1);
                //this.gridView1.OptionsSelection.EnableAppearanceFocusedRow = false;
                gridView1.EndUpdate();


                bs2 = new BindingSource(bs1, "FK_FaturaDetay_Fatura");
                bs3 = new BindingSource(bs2, "FK_FeatureKumas_BelgeDetay");
                bs4 = new BindingSource(bs2, "FK_Top_BelgeDetay");
                //bs5 = new BindingSource(bs1, "FK_FeatureAksesuar_Stok");

                gridControl2.DataSource = bs2;
                gridControl3.DataSource = bs4;
                InitGridView(gridView3);
                InitGridView(gridView4);

                gridControl4.DataSource = bs3;
                InitGridView(gridView5);

                /*
                DLCBind("dataLayoutControl1", bs3);
                UnitMeasureIdLookUpEdit.Properties.Assign(riUnitMeasure);
                */
                bs1.CurrentChanged += new EventHandler(bs1_CurrentChanged);
                bs3.CurrentChanged += new EventHandler(bs3_CurrentChanged);


                bs2.AddingNew += new AddingNewEventHandler(bs2_AddingNew);
                bs3.AddingNew += new AddingNewEventHandler(bs3_AddingNew);
                
                CurrentChanged(); //for initial state
            }
            catch (Exception Ex)
            {
            }


        }

        private void bs2_AddingNew(object sender, AddingNewEventArgs e)
        {
            bs1.EndEdit();
        }

        private void bs3_AddingNew(object sender, AddingNewEventArgs e)
        {
            bs1.EndEdit();
        }


        void bs1_CurrentChanged(object sender, EventArgs e)
        {
            Save();
            CurrentChanged();
        }

        void bs3_CurrentChanged(object sender, EventArgs e)
        {
            gridView5.OptionsView.NewItemRowPosition = (bs3.Position == -1 ? DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Top : DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None);
            gridView5.BestFitColumns();
        }

        void CurrentChanged()
        {
            if (bs1.Current != null)
            {
                faturaId = int.Parse(((DataRowView)bs1.Current).Row["FaturaId"].ToString());
                //this.Text = "Product : " + ((DataRowView)bs1.Current).Row["StokAdi"].ToString();
            }
            RefreshData();
        }

        private void RefreshData()
        {
            tableAdapterManager.FaturaDetayTableAdapter.FillByFaturaId(ds.FaturaDetay, faturaId);
            tableAdapterManager.FeatureKumasTableAdapter.FillByFaturaId(ds.FeatureKumas, faturaId);
            tableAdapterManager.RollTableAdapter.FillByFaturaId(ds.Roll, faturaId);

            gridView3.BestFitColumns();
            gridView4.BestFitColumns();

        }



    }
}